package yf.mall.controller;

import java.util.List;
import java.util.Map;

import com.yf.dao.SysRoleDao;
import com.yf.dao.SysRoleMenuDao;
import com.yf.dao.SysUserDao;
import com.yf.dao.SysUserRoleDao;
import com.yf.entity.SysRoleEntity;
import com.yf.entity.SysUserEntity;
import com.yf.request.SearchParam;
import com.yf.utils.ShiroUtils;
import yf.mall.service.MallStoreService;
import yf.mall.vo.MallStoreVo;
import yf.mall.vo.UserVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.UserEntity;
import yf.mall.service.UserService;
import com.yf.response.R;


/**
 * 用户Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Jul 11 12:00:51 CST 2019
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    /**
     * 列表用户
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:list")
    public R list(@RequestBody SearchParam<UserVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<UserVo> userList = userService.queryListVo(params.getEntity());
        Long userId = ShiroUtils.getUserId();
        List<Long> roleIdList = this.sysUserRoleDao.queryRoleIdList(userId);
        for (Long roleId : roleIdList) {
            SysRoleEntity roleEntity = sysRoleDao.get(roleId);
            if (roleEntity.getRoleName().equals("库房物流") || roleEntity.getRoleName().equals("市场部")) {
                return R.ok(new PageInfo<>(userList)).putExtra("type", 1);
            }
        }

        return R.ok(new PageInfo(userList));
    }

    /**
     * 查看用户
     */
    @SysLog("查看用户")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("user:info")
    public R info(@PathVariable("id") Long id) {
        UserVo user = userService.getVo(id);

        return R.ok(user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("user:save")
    public R save(UserEntity user) {
        userService.save(user);

        return R.ok(user);
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("user:update")
    public R update(@RequestBody UserVo userVo) {
        userService.updateVo(userVo);
        return R.ok(userVo);
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("user:delete")
    public R delete(Long[] ids) {
        userService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有用户
     */
    @SysLog("所有用户")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<UserEntity> userList = userService.queryList(params);

        return R.ok(userList);
    }

    /***********自定义开始***********/

    @Autowired
    private MallStoreService mallStoreService;

    //用户的产品账户
    @RequestMapping("/store")
    public R store(Long userId) {
        List<MallStoreVo> storeVos = mallStoreService.findByUserId(userId);
        return R.ok(storeVos);
    }
}
